package app.auto.designer.func;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import app.auto.designer.R;
import app.auto.designer.ActivityBase;
import app.auto.designer.ReturnActivity;
import app.auto.designer.func.FuncTypeActivity;
import app.auto.designer.intfs.SetIntfsActivity;
import app.auto.runner.base.MapViewConf;
import app.auto.runner.base.Util;
import app.auto.runner.base.action.ParamType;

public class ExpActivity extends ReturnActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_expression);
        List<String> funlist = Util.getClassName(this, "app.auto.runner.func", new ArrayList<String>(), false);
        String pkg = "app.auto.runner.func";

        List ms = new ArrayList();
        Map<String, String> http = null;
        for (String f : funlist) {
            Map<String, String> methodstring = new TreeMap<String, String>();

            String func = pkg + "." + f;
            ParamType pt = null;
            try {
                Class clz = Class.forName(func);

                pt = (ParamType) clz.getAnnotation(ParamType.class);

                if (pt == null) {
                    continue;
                }
                methodstring.put("desc", pt.desc());
                methodstring.put("type", pt.type().toString());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            methodstring.put("name", f);
            if (pt.desc().contains("服务器资源访问(http)")) {
                http = methodstring;

            } else {
                ms.add(methodstring);

            }

        }
        ms.add(0, http);

        MapViewConf mvf = MapViewConf.with(this).pair("desc->desc").source(R.layout.auto_string_left_right);
        MapViewConf.with(this).conf(mvf).source(ms, getView(R.id.list)).toView();
        ((ListView) getView(R.id.list)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Map map = (Map) parent.getItemAtPosition(position);
                if (map.get("name").toString().toLowerCase().equals("http")) {
                    startActivityForResult(new Intent(view.getContext(), SetIntfsActivity.class), 0);
                } else
                    startActivityForResult(new Intent(view.getContext(), FuncTypeActivity.class).putExtra("data", (Serializable) map), 0);
            }
        });

    }


    //参数模板
    //数据本地
    //

}
